package com.lihepeng.leecode.mulitithread;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

import java.time.Duration;
import java.util.HashSet;
import java.util.Set;

/**
 * 通过redis的乐观锁实现秒杀逻辑
 */
public class Miaosha {
    public static void main(String[] args) {
        GenericObjectPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxWait(Duration.ofMinutes(1000L));
        jedisPoolConfig.setMaxTotal(1000);

        Set<HostAndPort> nodes = new HashSet<>();

        nodes.add(new HostAndPort("192.168.124.5", 6379));

        nodes.add(new HostAndPort("192.168.124.5", 6380));

        nodes.add(new HostAndPort("192.168.124.5", 6381));
        JedisCluster jedisCluster = new JedisCluster(nodes,100,1000,100,"123456",jedisPoolConfig);
        jedisCluster.set("cluster-test", "my jedis cluster test");

        String result = jedisCluster.get("cluster-test");
        System.out.println(result);
        jedisCluster.close();

    }
}
